//
//  PopCancellationReasonView.m
//  NoneCar
//
//  Created by DongSen on 2018/7/17.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "TipsView.h"
#import "PopCancellationReasonView.h"

@interface PopCancellationReasonView ()<UITextViewDelegate>

@end

@implementation PopCancellationReasonView
+ (PopCancellationReasonView*)loadView{
    NSArray * viewArray =[[NSBundle mainBundle] loadNibNamed:@"PopCancellationReasonView" owner:self options:nil];
    PopCancellationReasonView * view  = [viewArray firstObject];
    view.backgroundColor =  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    view.bottomToView.constant = -300;
    view.reasonTextView.layer.masksToBounds = YES;
    view.reasonTextView.layer.cornerRadius = 2;
    view.reasonTextView.backgroundColor = STRCOLOR(@"#F4F4F4");
    view.confirmButton.layer.masksToBounds = YES;
    view.confirmButton.layer.cornerRadius = 4;
    view.reasonTextView.delegate = view;

    return view;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)showViews:(UIView*)view{
    [self setFrame:CGRectMake(0, 0, kSCREEN_W, kSCREEN_H)];
    if (!view) {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    else{
        [view addSubview:self];
    }
    WeakSelf(self);
    self.bottomToView.constant = -300;
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.bottomToView.constant =0;
                         [self layoutIfNeeded];
                         
                     }
                     completion:^(BOOL finished)
     {
         IQKeyboardManager * keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
         keyboardManager.enable = NO; // 控制整个功能是否启用
         [[NSNotificationCenter defaultCenter]addObserver:weakSelf selector:@selector(transformView:) name:UIKeyboardWillChangeFrameNotification object:nil];

         
     }];
}

-(void)remove{
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.bottomToView.constant =-300;
                         [self layoutIfNeeded];
                         
                     }
                     completion:^(BOOL finished)
     {
         [self removeFromSuperview];
         IQKeyboardManager * keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
         keyboardManager.enable = YES; // 控制整个功能是否启用
         
     }];
}
- (IBAction)closeButtonClicked:(UIButton *)sender {
    [self.reasonTextView resignFirstResponder];
    [self remove];
}
- (IBAction)confirmButtonClicked:(UIButton *)sender{
    [self.reasonTextView resignFirstResponder];
    [self remove];

}

-(void)transformView:(NSNotification *)aNSNotification
{
    //获取键盘弹出前的Rect
    NSValue *keyBoardBeginBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue *keyBoardEndBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect=[keyBoardEndBounds CGRectValue];
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY=endRect.origin.y-beginRect.origin.y;
    NSLog(@"看看这个变化的Y值:%f",deltaY);
    self.tops = deltaY;
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    [UIView animateWithDuration:0.25f animations:^{
//        self.bottomToView.constant =deltaY;
//        [self layoutIfNeeded];
        [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y+deltaY, self.frame.size.width, self.frame.size.height)];
    }];

}

- (void)textViewDidChange:(UITextView *)textView{
    NSString * string = [NSString stringWithFormat:@"%ld/100字",textView.text.length];
    if (textView.text.length>=100) {
        self.numberLabel.textColor = [UIColor redColor];
    }
    else{
        self.numberLabel.textColor = [UIColor darkGrayColor];
    }
    self.numberLabel.text =string;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView) {
        if (text.length == 0) return YES;
        NSInteger existedLength = textView.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = text.length;
        if (existedLength - selectedLength + replaceLength > 100)
        {
            [TipsView showMessage:@"最多输入100字"];
            return NO;
        }
    }
    return YES;
}
@end
